package com.me.dao.normal;

import com.me.dao.IUserDao;
import com.me.model.normal.NormalUser;
import com.me.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collection;
import java.util.UUID;

public class NormalUserDao implements IUserDao<NormalUser> {
	public static void main(String[] args) {
		//String id, String email, String username, String password, String headPath, String name, int sex,String mobile
		new NormalUserDao().addUser(new NormalUser("","","test2","123","","",1,""));
	}

	@Override
	public boolean addUser(NormalUser user) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "INSERT INTO `examsys`.`user` (`id`, `username`, `password`, `headPath`, `name`, `sex`, `admin`) VALUES (?, ?, ?, ?, ?, ?, ?);";

		try {
			int line = runner.update(
					sql,
					UUID.randomUUID().toString(), user.getUsername(), user.getPassword(),
					user.getHeadPath(), user.getName(), user.getSex(), user.isAdmin()
			);

			if (line != 1) {
				System.out.println("添加用户出错或失败");
				return false;
			}
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean isUserExist(String username) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.user where username = ?";

		try {
			return runner.query(sql, new BeanHandler<NormalUser>(NormalUser.class), username) != null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public NormalUser login(String uname, String pwd) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.user where username = ? and password = ?";

		try {
			return runner.query(sql, new BeanHandler<NormalUser>(NormalUser.class), uname, pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean isUserCanLogin(String uname, String pwd) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.user where username = ? and password = ?";

		try {
			return runner.query(sql, new BeanHandler<NormalUser>(NormalUser.class), uname, pwd) != null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(NormalUser user) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "UPDATE `examsys`.`user` SET `headPath` = ?, `name` = ?, `sex` = ?, `admin` = ? WHERE (`id` = ?);";
		try {
			int line = runner.update(
					sql,
					user.getHeadPath(), user.getName(), user.getSex(), user.isAdmin(),
					user.getId()
			);

			if (line != 1) {
				System.out.println("更新用户出错或失败");
				return false;
			}
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public Collection<NormalUser> getAll() {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.user";

		try {
			return runner.query(sql, new BeanListHandler<NormalUser>(NormalUser.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean remove(String id) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "delete FROM examsys.user where id=?";

		int line = 0;
		try {
			line = runner.update(sql,id);
			if (line != 1) {
				System.out.println("删除用户出错或失败");
				return false;
			}
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public NormalUser getById(String id) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.user where id = ?";

		try {
			return runner.query(sql, new BeanHandler<NormalUser>(NormalUser.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}


//	private Class<T> entityClass;
//
//	public NormalUserDao(Class<T> tClass) {
//		entityClass = tClass;
//	}
//
//	public boolean isUserExist(String username) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT * FROM examsys.user where username = ?";
//
//        try {
//            return runner.query(sql,new BeanHandler<T>(entityClass),username) != null;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    public T login(String uname, String pwd){
//	    QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//	    String sql = "SELECT * FROM examsys.user where username = ? and password = ?";
//
//        try {
//            return runner.query(sql,new BeanHandler<T>(entityClass),uname,pwd);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public boolean isUserCanLogin(String uname, String pwd) {
//	    QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//	    String sql = "SELECT * FROM examsys.user where username = ? and password = ?";
//
//	    try {
//		    return runner.query(sql,new BeanHandler<T>(entityClass),uname,pwd) != null;
//	    } catch (SQLException e) {
//		    e.printStackTrace();
//	    }
//
//        return false;
//    }
}
